package com.aim.Activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.widget.Toast;

import com.aim.api.AIMCaseConstants;
import com.aim.util.Preferences;
import com.aim.util.WebServiceUtil;

public class Settings extends PreferenceActivity implements
		Preference.OnPreferenceChangeListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		getPreferenceManager().setSharedPreferencesName(Preferences.NAME);
		addPreferencesFromResource(R.xml.preferences);
		EditTextPreference repasswordpreference = (EditTextPreference) findPreference(AIMCaseConstants.REPASSWORD);
		repasswordpreference.setOnPreferenceChangeListener(this);
		EditTextPreference passwordpreference = (EditTextPreference) findPreference(AIMCaseConstants.PASSWORD);
		passwordpreference.setOnPreferenceChangeListener(this);

		EditTextPreference validatecodepreference = (EditTextPreference) findPreference(AIMCaseConstants.VALIDATECODE);
		validatecodepreference.setOnPreferenceChangeListener(this);

		EditTextPreference usernamepreference = (EditTextPreference) findPreference(AIMCaseConstants.USERNAME);
		usernamepreference.setOnPreferenceChangeListener(this);

		EditTextPreference urlpreference = (EditTextPreference) findPreference(AIMCaseConstants.CONTEXTURL);
		urlpreference.setOnPreferenceChangeListener(this);
		
		ListPreference picpreference = (ListPreference) findPreference(AIMCaseConstants.PICSIZE);
		picpreference.setOnPreferenceChangeListener(this);

	}

	public boolean onPreferenceChange(Preference preference, Object newValue) {
		final String key = preference.getKey();
		boolean returnValue = true;
		if (key.equals(AIMCaseConstants.REPASSWORD)) {
			SharedPreferences sp = this.getSharedPreferences(Preferences.NAME, 0);
			String p1 = sp.getString(AIMCaseConstants.PASSWORD, null);
			Log.e("password",p1+" " );
			String p2 = sp.getString(AIMCaseConstants.REPASSWORD, null);
			Log.e("repassword",p2+" " );
			EditTextPreference passwordpreference = (EditTextPreference) findPreference(AIMCaseConstants.PASSWORD);
			if (newValue.equals(passwordpreference.getText())) {
				returnValue = true;
				EditTextPreference usernamepreference = (EditTextPreference) findPreference(AIMCaseConstants.USERNAME);
				
				String s1 = usernamepreference.getText();
		        String s2 = passwordpreference.getText();
		 		
		 		if (s1!=null&&s1!=null)
		 		{
		 			Log.e(s1, s2);
		 			String imei = "0";
		 			WebServiceUtil.setContext(this);
		 			Boolean b = WebServiceUtil.login("GetUser",s1,s2,imei);
		 					 		
		 		if(b){
		 			Toast.makeText(
							getApplicationContext(),
							getApplicationContext().getText(
									R.string.suceeslogin),
							Toast.LENGTH_LONG).show();
		 			}
		 		}
		 		else{
		 			Toast.makeText(
							getApplicationContext(),
							
									"Wrong Login infomation",
							Toast.LENGTH_LONG).show();
		 		}

				
	
			} else {
				Toast.makeText(
						getApplicationContext(),
						getApplicationContext().getText(
								R.string.dialog_password_mismatch),
						Toast.LENGTH_LONG).show();
				returnValue = false;
			}
		}

		
		if (returnValue) {
			WebServiceUtil.setContext(this.getApplicationContext());
		}
		return returnValue;

	}

}
